/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/09/04
*/
#include "TileLayout.h"
#include "../edui.h"

EDUI_NAMESPACE_BEGIN

TileLayout* TileLayout::GetInterface()
{
    TileLayout* tileLayout = new TileLayout;
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[TableLayout] new tileLayout : %d", tileLayout);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Add("TileLayout", tileLayout);
#endif
    return tileLayout;
}

void TileLayout::Release()
{
    this->Clear();
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[TileLayout] delete tileLayout : %s, %d", this->GetName(), this);
#endif
#ifdef EDUI_USE_LUAPLUS
    LuaMgrPtr->Remove(this);
#endif
    delete this;
}

TileLayout* TileLayout::ToTileLayout(Control* ctrl)
{
    return dynamic_cast<TileLayout*>(ctrl);
}

TileLayout::TileLayout() : m_columns(0)
{}

TileLayout::~TileLayout()
{}

void TileLayout::SetItemSize(int w, int h)
{
    if (w > 0) m_itemSize.w = w;
    if (h > 0) m_itemSize.h = h;
}

Size TileLayout::GetItemSize()
{
    return m_itemSize;
}

void TileLayout::SetColumns(int columns)
{
    if (columns > 0) m_columns = columns;
}

int TileLayout::GetColumns()
{
    return m_columns;
}

void TileLayout::AutoLayout()
{
    if (m_wnd->GetRoot() == this){
        m_rc.Reset(0, 0, m_wnd->GetWidth(), m_wnd->GetHeight());
    }
    Control::AutoLayout();

    if (m_items->size() == 0) return;

    Rect rc = m_rc;
    rc.x += m_rcInset.l;
    rc.y += m_rcInset.t;
    rc.w -= (m_rcInset.l + m_rcInset.r);
    rc.h -= (m_rcInset.t + m_rcInset.b);

    if (!m_columns && (m_itemSize.w == 0 || m_itemSize.h == 0)) return;

    int colmuns = m_columns;
    int itemw = m_itemSize.w;
    int itemh = m_itemSize.h;
    if (!colmuns) colmuns = (rc.w + m_childPadding.w) / (m_itemSize.w + m_childPadding.w);
    if (m_itemSize.w == 0 || m_itemSize.h == 0){
        itemw = (rc.w - (colmuns - 1) * m_childPadding.w) / colmuns;

        int line = (int)m_items->size() / colmuns;
        if (m_items->size() % colmuns != 0) ++line;
        itemh = (rc.h - (line - 1) * m_childPadding.h) / line;
    }

    Rect rctmp;
    int i = 0;
    int j = 0;
    for (ItemMap::iterator it = m_items->begin(); it != m_items->end();  ++it){
        if (!(*it) || !(*it)->IsVisible()) continue;
        if ((*it)->IsFloat()){
            (*it)->SetRect(&rc);
            (*it)->AutoLayout();
        } else {
            int y = rc.y + j * (itemh + m_childPadding.h);
            int h = itemh;
            if (y + h > rc.y + rc.h) h = rc.y + rc.h - y;
            
            rctmp.Reset(rc.x + i * (itemw + m_childPadding.w), y, itemw, h);

            (*it)->SetRect(&rctmp);
            (*it)->AutoLayout();

            ++i;
            if (i % colmuns == 0){
                ++j;
                i = 0;
            }
            // full
            if (j * (itemh + m_childPadding.h) >= rc.h) break;
        }
    }
}

void TileLayout::SetAttribute(const char* name, const char* value)
{
    if (0 == strcmp(name, "columns")){
        ParserInt(value, &m_columns, 0);
    } else if(0 == strcmp(name, "itemsize")){
        m_itemSize = ParserSize(value);
    } else {
        Container::SetAttribute(name, value);
    }
}

EDUI_NAMESPACE_END
